<?php

use Biz\service\salesRebate\SalesRebate as sSalesRebate;
use Biz\model\salesRebate\SalesPersonal as mSalesPersonal;
use Biz\validate\ShopCompanyInfo as checkAction;

class PersonAccountAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REALNAME;

    public function main()
    {
        $uid = $this->uid;

        $result = [];
        if ($add = $this->getRequest()->getPost('add', '')) {
            $add = json_decode($add, true);
            $time = date('Y-m-d H:i:s');
            $save = [
                'uid' => $uid,
                'bank_no' => $add['bank_no'],
                'telephone' => $add['telephone'],
            ];
            //修改和新增
            if (isset($add['id'])) {
                //编辑
                $where = ['id' => $add['id']];
                $save['edittime'] = $time;
                $result = $save;
                $result['id'] = $add['id'];
                try {
                    mSalesPersonal::update($save, $where);
                    $this->setSuccess($result);
                } catch (\Exception $e) {
                    throw new \Exception('修改失败');
                }
            } else {
                $result = sSalesRebate::isVaild($uid);
                if (!$result) {
                    throw new \Exception('账户未弃用');
                } else {
                    //新增
                    if (checkAction::checkIdCard($add['id_card_no'])) {
                        //第一次创建账户时，并创建提现申请
                        $save['addtime'] = $time;
                        $save['name'] = $add['name'];
                        $save['id_card_no'] = $add['id_card_no'];
                        $result = $save;

                        //添加白名单的账户不提现，已经有的账户并生效的从数组剔除
                        $user_list = mSalesPersonal::USER_LIST;
                        $salespersonal_id = mSalesPersonal::getAllByWhere('id', ['uid'=>$uid]);
                        if($salespersonal_id) {
                            unset($user_list[$uid]);
                        }
                        if(in_array($uid, array_keys($user_list))){
                            $save['status'] = 1;
                            $run = mSalesPersonal::save($save);
                            if($run){
                                $this->setSuccess($result);
                            }else{
                                throw new \Exception('创建失败');
                            }
                        }else {
                            $run = mSalesPersonal::save($save);
                            if ($run) {
                                $result['id'] = $run;
                                if (sSalesRebate::createBill($uid, 1)) {
                                    $this->setSuccess($result);
                                } else {
                                    mSalesPersonal::update(['status' => 9], ['id' => $run]);
                                    throw new \Exception('提现失败');
                                }
                            } else {
                                throw new \Exception('创建失败');
                            }
                        }
                    } else {
                        throw new \Exception('身份证错误');
                    }
                }
            }
        }
    }

}